<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索框 -->
      <el-input
        placeholder="请输入内容"
        v-model="params.query"
        clearable
        @clear="getOrderList()"
        @keyup.enter.native="getOrderList()"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="getOrderList()"
        ></el-button>
      </el-input>

      <!-- 订单表格区域 -->
      <el-table :data="orderList" style="width: 100%;" border stripe>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="240">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格"> </el-table-column>
        <el-table-column label="是否付款">
          <template #default="{row}">
            <el-tag
              v-if="row.order_pay === '0'"
              type="danger"
              effect="dark"
              size="mini"
              >未付款</el-tag
            >
            <el-tag v-else type="success" effect="dark" size="mini"
              >已付款</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"> </el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="180">
          <template #default="{row}">
            {{ row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{}">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditAddressDialog"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-location"
              size="mini"
              @click="showProgressBox"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pagenum"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="params.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="params.total"
      >
      </el-pagination>
    </el-card>

    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="editAddressDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <el-form
        :model="editAddressForm"
        :rules="editAddressFormRules"
        ref="editAddressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader :options="cityData" v-model="editAddressForm.address1">
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editAddressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAddressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAddressDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 展示物流进度的对话框 -->
    <el-dialog title="提示" :visible.sync="progressDialogVisible" width="50%">
      <span>这是一段信息</span>
    </el-dialog>
  </div>
</template>

<script>
// 导入citydata文件
import cityData from '../assets/citydata.js';
export default {
  data() {
    return {
      params: {
        query: '',
        pagenum: 1,
        pagesize: 10,
        total: 0,
      },
      orderList: [],
      // 控制修改地址对话框的显示和隐藏
      editAddressDialogVisible: false,
      // 修改地址的表单
      editAddressForm: {
        address1: [],
        address2: '',
      },
      // 修改地址的表单验证规则
      editAddressFormRules: {
        address1: [
          { required: true, message: '请选择省市区县', trigger: 'blur' },
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' },
        ],
      },
      cityData: cityData,
      // 控制物流进度显示的对话框
      progressDialogVisible: false,
      // 物流进度信息数据
      progressList: [],
    };
  },
  created() {
    this.getOrderList();
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.params,
      });
      if (res.meta.status !== 200) {
        return this.$Elementui.Message.error('获取订单数据失败！');
      }
      this.orderList = res.data.goods;
      this.params.total = res.data.total;
      // console.log(this.orderList);
    },
    // 控制每页显示多少条数据
    handleSizeChange(newSize) {
      this.params.pagesize = newSize;
      this.params.pagenum = 1;
      this.getOrderList();
    },
    // 控制当前的页码值
    handleCurrentChange(newNum) {
      this.params.pagenum = newNum;
      this.getOrderList();
    },
    // 显示出修改地址的对话框
    showEditAddressDialog() {
      this.editAddressDialogVisible = true;
    },
    // 监听修改地址对话框的关闭事件
    editDialogClosed() {
      this.$refs.editAddressFormRef.resetFields();
    },
    // 展示物流进度的对话框
    showProgressBox() {
      // 发起请求拿到物流进度的数据
      // const { data: res } = await this.$http.get('/kuaidi/1106975712662');
      // if (res.meta.status !== 200) {
      //   return this.$Elementui.Message.error('获取物流进度信息失败！');
      // }
      // this.progressList = res.data;
      // this.progressDialogVisible = true;
      // console.log(this.progressList);
      this.progressDialogVisible = true;
    },
  },
};
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 15px;
  .el-input {
    width: 350px;
  }
  .el-table {
    margin-top: 15px;
  }
  .el-pagination {
    margin-top: 15px;
  }
}
.el-cascader {
  width: 100%;
}
</style>
